package org.csu.mypetstore_seller.controller;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.csu.mypetstore_seller.domain.Order;
import org.csu.mypetstore_seller.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.List;

@Controller
@Slf4j
@RequestMapping("order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    HttpSession httpSession;//保存搜索条件

    @GetMapping("orderMain")
    public String orderMain(){
        log.info("访问订单管理主页面");
        return "order/ordermain";
    }

    //订单管理页面
    @GetMapping("order")
    public String order(Model model){
        model.addAttribute("orders", orderService.GetAllOrder());
        log.info("查看所有订单列表");
        return "order/order";
    }

    @GetMapping("searchorder")
    public String searchorder(Model model, @RequestParam("method") String method,@RequestParam(value = "str", required = false)String str){
        if(str!=null){
            //保存搜索条件，后面便可一直保持搜索状态
            httpSession.setAttribute("method",method);
            httpSession.setAttribute("str",str);

            List<Order> orders=orderService.GetOrder(method,str);
            if(orders==null||orders.size()==0||orders.get(0)==null){
                log.warn("查询不到订单信息，搜索方式：{}，关键词：{}", method, str);
                throw new IllegalArgumentException("未找到订单");
            }
            else {
                model.addAttribute("orders", orders);
                log.info("订单查询成功，找到{}条记录", orders.size());
                return "order/order";
            }
        }
        else{
            log.info("搜索条件为空，显示所有订单");
            return "order/order";
        }
    }

    @GetMapping("deleteOrder")
    public String deleteOrder(@RequestParam("orderid")String id, Model model){
        log.info("开始删除订单，订单ID：{}", id);
        orderService.DeleteOrderByOrderId(Integer.parseInt(id));
        log.info("订单{}删除成功", id);
        return getString(model);
    }

    //辅助，获取搜索条件
    private String getString(Model model) {
        //如果之前有保存的搜索条件
        if(httpSession.getAttribute("str")!=null){
            String str=httpSession.getAttribute("str").toString();
            String method=httpSession.getAttribute("method").toString();
            log.info("使用搜索条件重新加载订单列表，搜索方式：{}，关键词：{}", method, str);
            
            List<Order> orders=orderService.GetOrder(method,str);
            if(orders==null){
                log.warn("根据搜索条件未找到订单");
                return "order/error";
            }
            else{
                model.addAttribute("orders",orders);//显示带搜索条件的列表
                log.info("成功加载订单列表，共{}条记录", orders.size());
                return "order/order";
            }
        }
        else{
            List<Order> orders=orderService.GetAllOrder();
            model.addAttribute("orders",orders);
            log.info("加载所有订单列表，共{}条记录", orders.size());
            return "order/order";
        }
    }

    //进入订单修改页面
//    @GetMapping("updateOrder")
//    public String searchUpdateOrder(@RequestParam("orderid")String id,Model model){
//        Order order=orderService.GetOrderByOrderId(Integer.parseInt(id));//获取指定订单
//        model.addAttribute("order",order);//传递订单信息
//        log.info("查看订单{}的详细信息", id);
//        return "order/updateorder";
//    }
    @GetMapping("updateOrder")
    public String searchUpdateOrder(
            @RequestParam("orderid") String id,
            Model model) {
        // 校验订单编号是否为空
        if (id == null || id.trim().isEmpty()) {
            throw new IllegalArgumentException("订单编号不能为空");
        }

        // 查询订单
        Order order = orderService.GetOrderByOrderId(Integer.parseInt(id));
        if (order == null) {
            // 如果订单不存在，抛出异常或返回错误信息
            throw new IllegalArgumentException("未找到订单编号为 " + id + " 的订单");
        }else {
            // 传递订单信息到视图
            model.addAttribute("order", order);
            log.info("查看订单{}的详细信息", id);
            return "order/updateorder";
        }


    }

    //提交订单修改
    @PostMapping("updateOrder")
    public String updateOrder(@ModelAttribute Order order, @RequestParam("orderId")String id,Model model){
        orderService.UpdateOrder(order,id);
        log.info("订单{}信息更新成功", order.getOrderId());
        //return getString(model);
        List<Order> orders=orderService.GetAllOrder();
        model.addAttribute("orders", orders);
        return "order/order";
    }

}

